package javacafe.daoimpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javacafe.dao.StudyDAO;
import javacafe.dto.study.StudyAuthMgrDTO;
import javacafe.dto.study.StudyDTO;
import javacafe.ibatis.SqlDaoSuport;

public class StudyDAOImpl extends SqlDaoSuport implements StudyDAO {
	
	private static StudyDAO dao = null;
	
	public static StudyDAO getInstance(){
		if (dao == null) {
			dao = new StudyDAOImpl();
		}
		return dao;
	}

	@Override
	public void createStudy(StudyDTO dto) {
		try {
			getSqlMapper().insert("createStudy", dto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<StudyDTO> getStudyList(int m_no) {
		
		List<StudyDTO> list = null;
		
		try {
			if (m_no > 0) {
				list = getSqlMapper().queryForList("getStudyList", m_no);
			}else{
				list = getSqlMapper().queryForList("getStudyList");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public StudyDTO getStudyInfo(int study_no) {
		StudyDTO dto = null;
		try {
			dto = (StudyDTO)getSqlMapper().queryForObject("getStudyInfo", study_no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dto;
	}

	@Override
	public void setBackground(Map<String, Object> map) {
		try {
			getSqlMapper().update("setStudyBackground", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<StudyAuthMgrDTO> getStudyMemberAuthList(Map<String, Object> map) {
		List<StudyAuthMgrDTO> list = null;
		
		try {
			list = getSqlMapper().queryForList("getStudyMemberAuthList", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public void updateAuth(Map<String, Object> map) {
		try {
			getSqlMapper().update("updateStudyAuth", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void withdrawalStudyMember(Map<String, Object> map) {
		try {
			getSqlMapper().update("withdrawalStudyMember", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
